package com.curd.demo.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.curd.demo.common.Result;
import com.curd.demo.entity.User;
import com.curd.demo.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts")
public class EchartsController {


    //死数据接口
    @GetMapping("/example")
    public Result get(){

        Map<String,Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));//ctrl+r批量替换
        map.put("y",CollUtil.newArrayList(150, 230, 224, 218, 135, 147, 260));

        return Result.success(map);
    }

    @Resource//    @RequestBody就不会走视图解析器，直接返回一个字符串
    //不然返回的是空，获取不到数据
    private IUserService userService;

    @GetMapping("/members")
    public Result membersget(){
        List<User> list = userService.list();
        int q1 = 0;
        int q2 = 0;
        int q3 = 0;
        int q4 = 0;
        for (User user : list){
            Date createTime = user.getTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);//分成四分
            switch(quarter){
                case Q1: q1 +=1;break;
                case Q2: q2 +=1;break;
                case Q3: q3 +=1;break;
                case Q4: q4 +=1;break;
                default: break;
            }

        }

        return Result.success(CollUtil.newArrayList(q1,q2,q3,q4));
    }
}
